package com.yanghui.eletask4.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import com.yanghui.eletask4.utils.LayoutTool
import java.util.*

class HeartBeatGraph(context: Context, attr: AttributeSet) : View(context, attr) {

    val dataRange = 10000.0
    val dataNum = 250
    val dataArray = LinkedList<Double>()
    lateinit var linePainter: Paint
    var OUTER_WIDTH_SIZE: Int = 0
    var OUTER_HEIGHT_SIZE: Int = 0
    var realWidth: Int = 0
    var realHeight: Int = 0

    init {
        OUTER_WIDTH_SIZE = LayoutTool.dip2px(context, 260.0f)
        OUTER_HEIGHT_SIZE = LayoutTool.dip2px(context, 100.0f)
        setBackgroundColor(Color.rgb(223, 228, 234))
        linePainter = Paint()
        linePainter.strokeWidth = 3F
        linePainter.color = Color.BLACK
        linePainter.isAntiAlias = true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(
            measureWidth(widthMeasureSpec),
            measureHeight(heightMeasureSpec)
        )
    }

    private fun measureWidth(widthMeasureSpec: Int): Int {
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthVal = MeasureSpec.getSize(widthMeasureSpec)
        //处理三种模式
        return if (widthMode == MeasureSpec.EXACTLY) {
            widthVal + paddingLeft + paddingRight
        } else if (widthMode == MeasureSpec.UNSPECIFIED) {
            OUTER_WIDTH_SIZE
        } else {
            OUTER_WIDTH_SIZE.coerceAtMost(widthVal)
        }
    }

    /* 测量高度 */
    private fun measureHeight(heightMeasureSpec: Int): Int {
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightVal = MeasureSpec.getSize(heightMeasureSpec)
        //处理三种模式
        return if (heightMode == MeasureSpec.EXACTLY) {
            heightVal + paddingTop + paddingBottom
        } else if (heightMode == MeasureSpec.UNSPECIFIED) {
            OUTER_HEIGHT_SIZE
        } else {
            OUTER_HEIGHT_SIZE.coerceAtMost(heightVal)
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        realWidth = w
        realHeight = h
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        for (i in 0 until dataArray.size - 1) {
            canvas?.drawLine(
                (realWidth * i / (dataNum - 1)).toFloat(),
                (realHeight - realHeight / dataRange * dataArray[i]).toFloat(),
                (realWidth * (i + 1) / (dataNum - 1)).toFloat(),
                (realHeight - realHeight / dataRange * dataArray[i + 1]).toFloat(),
                linePainter
            )
        }
    }

    fun addData(data: Double) {
        if (dataArray.size < dataNum) {
            dataArray.addLast(data)
        } else {
            dataArray.removeFirst()
            dataArray.addLast(data)
        }
        invalidate()
    }

    fun clearData() {
        dataArray.clear()
        invalidate()
    }
}